Imports System.Data.SqlClient

Public Class cls_vehiculo_

    Implements ICloneable

#Region "Variables ..."
    Private int_idvehiculo As Integer = 0
    Private int_idmarca As Integer = 0
    Private int_idmodelo As Integer = 0
    Private int_año As Integer = 0
    Private str_patente As String = ""
    Private int_idpersonal As Integer = 0
    Private int_idcentrocostoorden As Integer = 0
    Private str_inventario As String = ""
    Private str_denominacion As String = ""
    Private int_idvehiculotipo As Integer = 0
    Private int_idregistro As Integer = 0
    Private int_idcolor As Integer = 0

    Private obj_registro As cls_registro_
    Private obj_marca As cls_marca_
    Private obj_modelo As cls_modelo_
    Private obj_vehiculotipo As cls_vehiculotipo_
    Private obj_color As cls_color_
    Private obj_personal As cls_personal_
    Private obj_telepeaje_ As cls_telepeaje_

    Private col_Items As Collections.Generic.List(Of cls_vehiculo_) = Nothing
#End Region

    Public Overrides Function ToString() As String
        Return patente
    End Function
#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idvehiculo
        End Get
    End Property
    Property idvehiculo() As Integer
        Get
            idvehiculo = int_idvehiculo
        End Get
        Set(ByVal value As Integer)
            int_idvehiculo = value
        End Set
    End Property
    Property idmarca() As Integer
        Get
            idmarca = int_idmarca
        End Get
        Set(ByVal value As Integer)
            int_idmarca = value
        End Set
    End Property
    Property idmodelo() As Integer
        Get
            idmodelo = int_idmodelo
        End Get
        Set(ByVal value As Integer)
            int_idmodelo = value
        End Set
    End Property
    Property año() As Integer
        Get
            año = int_año
        End Get
        Set(ByVal value As Integer)
            int_año = value
        End Set
    End Property
    Property patente() As String
        Get
            patente = str_patente
        End Get
        Set(ByVal value As String)
            str_patente = value
        End Set
    End Property
    Property idpersonal() As Integer
        Get
            idpersonal = int_idpersonal
        End Get
        Set(ByVal value As Integer)
            int_idpersonal = value
        End Set
    End Property
    Property idcentrocostoorden() As Integer
        Get
            idcentrocostoorden = int_idcentrocostoorden
        End Get
        Set(ByVal value As Integer)
            int_idcentrocostoorden = value
        End Set
    End Property
    Property inventario() As String
        Get
            inventario = str_inventario
        End Get
        Set(ByVal value As String)
            str_inventario = value
        End Set
    End Property
    Property denominacion() As String
        Get
            denominacion = str_denominacion
        End Get
        Set(ByVal value As String)
            str_denominacion = value
        End Set
    End Property
    Property idvehiculotipo() As Integer
        Get
            idvehiculotipo = int_idvehiculotipo
        End Get
        Set(ByVal value As Integer)
            int_idvehiculotipo = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property
    Property idcolor() As Integer
        Get
            idcolor = int_idcolor
        End Get
        Set(ByVal value As Integer)
            int_idcolor = value
        End Set
    End Property
    Property telepeaje_() As cls_telepeaje_
        Get
            If IsNothing(obj_telepeaje_) Then
                obj_telepeaje_ = New cls_telepeaje_
                If Not obj_telepeaje_.CargarXidvehiculo(idvehiculo) Then
                    obj_telepeaje_ = Nothing
                End If
            End If
            telepeaje_ = obj_telepeaje_
        End Get
        Set(ByVal value As cls_telepeaje_)
            obj_telepeaje_ = value
        End Set
    End Property
    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property
    Property marca() As cls_marca_
        Get
            If IsNothing(obj_marca) Then
                obj_marca = New cls_marca_
                If Not obj_marca.Buscar(idmarca) Then
                    obj_marca = Nothing
                End If
            End If
            marca = obj_marca
        End Get
        Set(ByVal value As cls_marca_)
            obj_marca = value
        End Set
    End Property
    Property color() As cls_color_
        Get
            If IsNothing(obj_color) Then
                obj_color = New cls_color_
                If Not obj_color.Buscar(idcolor) Then
                    obj_color = Nothing
                End If
            End If
            Color = obj_color
        End Get
        Set(ByVal value As cls_color_)
            obj_color = value
        End Set
    End Property
    Property modelo() As cls_modelo_
        Get
            If IsNothing(obj_modelo) Then
                obj_modelo = New cls_modelo_
                If Not obj_modelo.Buscar(idmodelo) Then
                    obj_modelo = Nothing
                End If
            End If
            modelo = obj_modelo
        End Get
        Set(ByVal value As cls_modelo_)
            obj_modelo = value
        End Set
    End Property
    Property vehiculotipo() As cls_vehiculotipo_
        Get
            If IsNothing(obj_vehiculotipo) Then
                obj_vehiculotipo = New cls_vehiculotipo_
                If Not obj_vehiculotipo.Buscar(idvehiculotipo) Then
                    obj_vehiculotipo = Nothing
                End If
            End If
            vehiculotipo = obj_vehiculotipo
        End Get
        Set(ByVal value As cls_vehiculotipo_)
            obj_vehiculotipo = value
        End Set
    End Property
    Property personal() As cls_personal_
        Get
            If IsNothing(obj_personal) Then
                obj_personal = New cls_personal_
                If Not obj_personal.Buscar(idpersonal) Then
                    obj_personal = Nothing
                End If
            End If
            personal = obj_personal
        End Get
        Set(ByVal value As cls_personal_)
            obj_personal = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_vehiculo_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_vehiculo_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_vehiculo_)
        While obj_Reader.Read()
            Dim Elemento As New cls_vehiculo_
            Elemento.int_idvehiculo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarca"))) = False Then
                Elemento.int_idmarca = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarca")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodelo"))) = False Then
                Elemento.int_idmodelo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodelo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("año"))) = False Then
                Elemento.int_año = (obj_Reader.GetValue(obj_Reader.GetOrdinal("año")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("patente"))) = False Then
                Elemento.str_patente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("patente")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idpersona"))) = False Then
                Elemento.int_idpersonal = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idpersona")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcentrocostoorden"))) = False Then
                Elemento.int_idcentrocostoorden = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcentrocostoorden")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("inventario"))) = False Then
                Elemento.str_inventario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("inventario")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("denominacion"))) = False Then
                Elemento.str_denominacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("denominacion")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculotipo"))) = False Then
                Elemento.int_idvehiculotipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculotipo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcolor"))) = False Then
                Elemento.int_idcolor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcolor")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idvehiculo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarca"))) = False Then
            int_idmarca = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmarca")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodelo"))) = False Then
            int_idmodelo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmodelo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("año"))) = False Then
            int_año = (obj_Reader.GetValue(obj_Reader.GetOrdinal("año")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("patente"))) = False Then
            str_patente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("patente")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idpersona"))) = False Then
            int_idpersonal = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idpersona")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcentrocostoorden"))) = False Then
            int_idcentrocostoorden = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcentrocostoorden")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("inventario"))) = False Then
            str_inventario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("inventario")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("denominacion"))) = False Then
            str_denominacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("denominacion")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculotipo"))) = False Then
            int_idvehiculotipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculotipo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcolor"))) = False Then
            int_idcolor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcolor")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try

            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "vehiculo__Nuevo"
                .Parameters.Add("@idmarca", SqlDbType.Int).Value = int_idmarca
                .Parameters.Add("@idmodelo", SqlDbType.Int).Value = int_idmodelo
                .Parameters.Add("@año", SqlDbType.Int).Value = int_año
                .Parameters.Add("@patente", SqlDbType.NVarChar).Value = str_patente
                .Parameters.Add("@idpersona", SqlDbType.Int).Value = int_idpersonal
                .Parameters.Add("@idcentrocostoorden", SqlDbType.Int).Value = int_idcentrocostoorden
                .Parameters.Add("@inventario", SqlDbType.NVarChar).Value = str_inventario
                .Parameters.Add("@denominacion", SqlDbType.NVarChar).Value = str_denominacion
                .Parameters.Add("@idvehiculotipo", SqlDbType.Int).Value = int_idvehiculotipo
                .Parameters.Add("@idcolor", SqlDbType.Int).Value = int_idcolor
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de vehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "vehiculo__Modificar"
                .Parameters.Add("@idvehiculo", SqlDbType.Int).Value = int_idvehiculo
                .Parameters.Add("@idmarca", SqlDbType.Int).Value = int_idmarca
                .Parameters.Add("@idmodelo", SqlDbType.Int).Value = int_idmodelo
                .Parameters.Add("@año", SqlDbType.Int).Value = int_año
                .Parameters.Add("@patente", SqlDbType.NVarChar).Value = str_patente
                .Parameters.Add("@idpersona", SqlDbType.Int).Value = int_idpersonal
                .Parameters.Add("@idcentrocostoorden", SqlDbType.Int).Value = int_idcentrocostoorden
                .Parameters.Add("@inventario", SqlDbType.NVarChar).Value = str_inventario
                .Parameters.Add("@denominacion", SqlDbType.NVarChar).Value = str_denominacion
                .Parameters.Add("@idvehiculotipo", SqlDbType.Int).Value = int_idvehiculotipo
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@idcolor", SqlDbType.Int).Value = int_idcolor
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de vehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idvehiculo As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "vehiculo__Eliminar"
                .Parameters.Add("@idvehiculo", SqlDbType.Int).Value = idvehiculo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de vehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idvehiculo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "vehiculo__Buscar"
                .Parameters.Add("@idvehiculo", SqlDbType.Int).Value = idvehiculo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de vehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Function BuscarXpatente(ByVal prm_patente As String) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            BuscarXpatente = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "vehiculo__BuscarXpatente"
                .Parameters.Add("@patente", SqlDbType.NVarChar).Value = prm_patente
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                BuscarXpatente = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al BuscarXpatente el registro de vehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "vehiculo__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de vehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "vehiculo__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de vehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_vehiculo_Clon As New cls_vehiculo_
        obj_vehiculo_Clon.int_idvehiculo = Me.int_idvehiculo
        obj_vehiculo_Clon.int_idmarca = Me.int_idmarca
        obj_vehiculo_Clon.int_idmodelo = Me.int_idmodelo
        obj_vehiculo_Clon.int_año = Me.int_año
        obj_vehiculo_Clon.str_patente = Me.str_patente
        obj_vehiculo_Clon.int_idpersonal = Me.int_idpersonal
        obj_vehiculo_Clon.int_idcentrocostoorden = Me.int_idcentrocostoorden
        obj_vehiculo_Clon.str_inventario = Me.str_inventario
        obj_vehiculo_Clon.str_denominacion = Me.str_denominacion
        obj_vehiculo_Clon.int_idvehiculotipo = Me.int_idvehiculotipo
        obj_vehiculo_Clon.int_idcolor = Me.int_idcolor
        obj_vehiculo_Clon.col_Items = Me.col_Items
        Return obj_vehiculo_Clon
    End Function

End Class
